<!DOCTYPE html><html><head><meta charset="utf-8"><title>window服务编程 | Lehoon</title><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta name="description" content="window下编写服务程序样例。"><meta property="og:type" content="article"><meta property="og:title" content="window服务编程"><meta property="og:url" content="http://blog.lehoon.cn/backend/2015/09/28/window-service-program.html"><meta property="og:site_name" content="Lehoon"><meta property="og:description" content="window下编写服务程序样例。"><meta property="og:updated_time" content="2016-07-28T01:53:58.623Z"><meta name="twitter:card" content="summary"><meta name="twitter:title" content="window服务编程"><meta name="twitter:description" content="window下编写服务程序样例。"><link rel="alternative" href="/rss.xml" title="Lehoon" type="application/atom+xml"><link rel="icon" href="/favicon.ico"><link rel="stylesheet" href="/css/style.css"><link rel="stylesheet" href="http://static.lehoon.cn/font-awesome.min.css"><link rel="apple-touch-icon" href="/img/apple-touch-icon.png"><link rel="stylesheet" href="http://static.lehoon.cn/fancybox/jquery.fancybox.css"><script src="http://static.lehoon.cn/js/pace.js"></script><link href="/css/pace/pace-theme-flash.css" rel="stylesheet"><script>var yiliaConfig={rootUrl:"/",fancybox:!0,animate:!1,isHome:!1,isPost:!0,isArchive:!1,isTag:!1,isCategory:!1,open_in_new:!1}</script></head><body><div id="container"><div class="left-col"><div class="overlay"></div><div class="intrude-less"><header id="header" class="inner"><hgroup><h1 class="header-author"><a href="/" title="Hi Lehoon">lehoon</a></h1></hgroup><div id="switch-area" class="switch-area"><div class="switch-wrap"><section class="switch-part switch-part1"><nav class="header-menu"><ul><li><a href="/">主页</a></li><li><a href="/archives">文章</a></li><li><a href="/projects">项目</a></li><li><a href="/categories/backend">后端</a></li><li><a href="/categories/frontend">前端</a></li><li><a href="/categories/orther">其他</a></li><li><a href="/polymerization">聚合</a></li><li><a href="/about">关于</a></li></ul></nav><nav class="header-nav"><ul class="social"><a class="fl mail" target="_blank" href="mailto:lehoon@lehoon.com" title="mail">mail</a> <a class="fl github" target="_blank" href="https://github.com/lehoon" title="github">github</a> <a class="fl weibo" target="_blank" href="http://weibo.com/lehoons" title="weibo">weibo</a> <a class="fl rss" target="_blank" href="/rss.xml" title="rss">rss</a></ul></nav></section><section class="switch-part switch-part3"><div id="js-aboutme">热爱生活，享受生活。</div></section></div></div></header></div></div><div class="mid-col"><nav id="mobile-nav"><div class="overlay"><div class="slider-trigger"></div><h1 class="header-author js-mobile-header hide"><a href="/" title="Me">lehoon</a></h1></div><div class="intrude-less"><header id="header" class="inner"><hgroup><h1 class="header-author"><a href="/" title="Me">lehoon</a></h1></hgroup><nav class="header-menu"><ul><li><a href="/">主页</a></li><li><a href="/archives">文章</a></li><li><a href="/projects">项目</a></li><li><a href="/categories/backend">后端</a></li><li><a href="/categories/frontend">前端</a></li><li><a href="/categories/orther">其他</a></li><li><a href="/polymerization">聚合</a></li><li><a href="/about">关于</a></li><div class="clearfix"></div></ul></nav><nav class="header-nav"><div class="social"><a class="mail" target="_blank" href="mailto:lehoon@lehoon.com" title="mail">mail</a> <a class="github" target="_blank" href="https://github.com/lehoon" title="github">github</a> <a class="weibo" target="_blank" href="http://weibo.com/lehoons" title="weibo">weibo</a> <a class="rss" target="_blank" href="/rss.xml" title="rss">rss</a></div></nav></header></div></nav><div class="body-wrap"><article id="post-window-service-program" class="article article-type-post" itemscope itemprop="blogPost"><div class="article-meta"><a href="/backend/2015/09/28/window-service-program.html" class="article-date"><time datetime="2015-09-28T11:40:18.000Z" itemprop="datePublished">2015-09-28</time></a></div><div class="article-inner"><input type="hidden" class="isFancy"><header class="article-header"><h1 class="article-title" itemprop="name">window服务编程</h1></header><div class="article-info article-info-post"><div class="article-category tagcloud"><a class="article-category-link" href="/categories/backend/">后端</a></div><div class="article-tag tagcloud"><ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/c/">c</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/c/">c++</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/service/">service</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/window/">window</a></li></ul></div><div class="clearfix"></div></div><div class="article-entry" itemprop="articleBody"><p>window下编写服务程序样例。<br><a id="more"></a><br></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div><div class="line">224</div><div class="line">225</div><div class="line">226</div><div class="line">227</div><div class="line">228</div><div class="line">229</div><div class="line">230</div><div class="line">231</div><div class="line">232</div><div class="line">233</div><div class="line">234</div><div class="line">235</div><div class="line">236</div><div class="line">237</div><div class="line">238</div><div class="line">239</div><div class="line">240</div><div class="line">241</div><div class="line">242</div><div class="line">243</div><div class="line">244</div><div class="line">245</div><div class="line">246</div><div class="line">247</div><div class="line">248</div><div class="line">249</div><div class="line">250</div><div class="line">251</div><div class="line">252</div><div class="line">253</div><div class="line">254</div><div class="line">255</div><div class="line">256</div><div class="line">257</div><div class="line">258</div><div class="line">259</div><div class="line">260</div><div class="line">261</div><div class="line">262</div><div class="line">263</div><div class="line">264</div><div class="line">265</div><div class="line">266</div><div class="line">267</div><div class="line">268</div><div class="line">269</div><div class="line">270</div><div class="line">271</div><div class="line">272</div><div class="line">273</div><div class="line">274</div><div class="line">275</div><div class="line">276</div><div class="line">277</div><div class="line">278</div><div class="line">279</div><div class="line">280</div><div class="line">281</div><div class="line">282</div><div class="line">283</div><div class="line">284</div><div class="line">285</div><div class="line">286</div><div class="line">287</div><div class="line">288</div><div class="line">289</div><div class="line">290</div><div class="line">291</div><div class="line">292</div><div class="line">293</div><div class="line">294</div><div class="line">295</div><div class="line">296</div><div class="line">297</div><div class="line">298</div><div class="line">299</div><div class="line">300</div><div class="line">301</div><div class="line">302</div><div class="line">303</div><div class="line">304</div><div class="line">305</div><div class="line">306</div><div class="line">307</div><div class="line">308</div><div class="line">309</div><div class="line">310</div><div class="line">311</div><div class="line">312</div><div class="line">313</div><div class="line">314</div><div class="line">315</div><div class="line">316</div><div class="line">317</div><div class="line">318</div><div class="line">319</div><div class="line">320</div><div class="line">321</div><div class="line">322</div><div class="line">323</div><div class="line">324</div><div class="line">325</div><div class="line">326</div><div class="line">327</div><div class="line">328</div><div class="line">329</div><div class="line">330</div><div class="line">331</div><div class="line">332</div><div class="line">333</div><div class="line">334</div><div class="line">335</div><div class="line">336</div><div class="line">337</div><div class="line">338</div><div class="line">339</div><div class="line">340</div><div class="line">341</div><div class="line">342</div><div class="line">343</div><div class="line">344</div><div class="line">345</div><div class="line">346</div><div class="line">347</div><div class="line">348</div><div class="line">349</div><div class="line">350</div><div class="line">351</div><div class="line">352</div><div class="line">353</div><div class="line">354</div><div class="line">355</div><div class="line">356</div><div class="line">357</div><div class="line">358</div><div class="line">359</div><div class="line">360</div><div class="line">361</div><div class="line">362</div><div class="line">363</div><div class="line">364</div><div class="line">365</div><div class="line">366</div><div class="line">367</div><div class="line">368</div><div class="line">369</div><div class="line">370</div><div class="line">371</div><div class="line">372</div><div class="line">373</div><div class="line">374</div><div class="line">375</div><div class="line">376</div><div class="line">377</div><div class="line">378</div><div class="line">379</div><div class="line">380</div><div class="line">381</div><div class="line">382</div><div class="line">383</div><div class="line">384</div><div class="line">385</div><div class="line">386</div><div class="line">387</div><div class="line">388</div><div class="line">389</div><div class="line">390</div><div class="line">391</div><div class="line">392</div><div class="line">393</div><div class="line">394</div><div class="line">395</div><div class="line">396</div><div class="line">397</div><div class="line">398</div><div class="line">399</div><div class="line">400</div><div class="line">401</div><div class="line">402</div><div class="line">403</div><div class="line">404</div><div class="line">405</div><div class="line">406</div><div class="line">407</div><div class="line">408</div><div class="line">409</div><div class="line">410</div><div class="line">411</div><div class="line">412</div><div class="line">413</div><div class="line">414</div><div class="line">415</div><div class="line">416</div><div class="line">417</div><div class="line">418</div><div class="line">419</div><div class="line">420</div><div class="line">421</div><div class="line">422</div><div class="line">423</div><div class="line">424</div><div class="line">425</div><div class="line">426</div><div class="line">427</div><div class="line">428</div><div class="line">429</div><div class="line">430</div><div class="line">431</div><div class="line">432</div><div class="line">433</div><div class="line">434</div><div class="line">435</div><div class="line">436</div><div class="line">437</div><div class="line">438</div><div class="line">439</div><div class="line">440</div><div class="line">441</div><div class="line">442</div><div class="line">443</div><div class="line">444</div><div class="line">445</div><div class="line">446</div><div class="line">447</div><div class="line">448</div><div class="line">449</div><div class="line">450</div><div class="line">451</div><div class="line">452</div><div class="line">453</div><div class="line">454</div><div class="line">455</div><div class="line">456</div><div class="line">457</div><div class="line">458</div><div class="line">459</div><div class="line">460</div><div class="line">461</div><div class="line">462</div><div class="line">463</div><div class="line">464</div><div class="line">465</div><div class="line">466</div><div class="line">467</div><div class="line">468</div><div class="line">469</div><div class="line">470</div><div class="line">471</div><div class="line">472</div><div class="line">473</div><div class="line">474</div><div class="line">475</div><div class="line">476</div><div class="line">477</div><div class="line">478</div><div class="line">479</div><div class="line">480</div><div class="line">481</div><div class="line">482</div><div class="line">483</div><div class="line">484</div><div class="line">485</div><div class="line">486</div><div class="line">487</div><div class="line">488</div><div class="line">489</div><div class="line">490</div><div class="line">491</div><div class="line">492</div><div class="line">493</div><div class="line">494</div><div class="line">495</div><div class="line">496</div><div class="line">497</div><div class="line">498</div><div class="line">499</div><div class="line">500</div><div class="line">501</div><div class="line">502</div><div class="line">503</div><div class="line">504</div><div class="line">505</div><div class="line">506</div><div class="line">507</div><div class="line">508</div><div class="line">509</div><div class="line">510</div><div class="line">511</div><div class="line">512</div><div class="line">513</div><div class="line">514</div><div class="line">515</div><div class="line">516</div><div class="line">517</div><div class="line">518</div><div class="line">519</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">pragma</span> once</span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;tchar.h&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;comutil.h&gt;</span></span></div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> SLEEP_TIME 5000</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> LOGFILE <span class="meta-string">"d:\\ITXBaseProviderService.txt"</span></span></div><div class="line"></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="keyword">char</span>    * ServiceName=<span class="string">"ITXBaseProviderService"</span>;</div><div class="line"></div><div class="line"><span class="keyword">enum</span> EnumRunState</div><div class="line">&#123;</div><div class="line">     None=<span class="number">0</span>,</div><div class="line">     Running=<span class="number">1</span>,</div><div class="line">     Stoped=<span class="number">2</span>,</div><div class="line">     Pause=<span class="number">3</span>,</div><div class="line">&#125;;</div><div class="line"></div><div class="line">EnumRunState RunState;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">WriteToLog</span><span class="params">(<span class="keyword">char</span>* str)</span></span></div><div class="line">&#123;</div><div class="line">    FILE* <span class="built_in">log</span>;</div><div class="line">    <span class="built_in">log</span>=fopen(LOGFILE,<span class="string">"a+"</span>);</div><div class="line">    <span class="keyword">if</span>(<span class="built_in">log</span>==<span class="literal">NULL</span>)  <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">    <span class="built_in">fprintf</span>(<span class="built_in">log</span>,<span class="string">"%s\n"</span>,str);</div><div class="line">    fclose(<span class="built_in">log</span>);</div><div class="line">    <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//定义变量和方法</span></div><div class="line">SERVICE_STATUS ServiceStatus;</div><div class="line">SERVICE_STATUS_HANDLE hStatus;</div><div class="line">HANDLE terminateEvent; <span class="comment">//事件</span></div><div class="line">HANDLE threadHandle;   <span class="comment">//线程</span></div><div class="line"></div><div class="line"><span class="function">VOID <span class="title">terminate</span><span class="params">(DWORD error)</span></span>;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">ServiceMain</span><span class="params">(<span class="keyword">int</span> argc,<span class="keyword">char</span>** argv)</span></span>;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">ControlHandler</span><span class="params">(DWORD request)</span></span>;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">InitService</span><span class="params">()</span></span>;</div><div class="line"><span class="comment">//</span></div><div class="line"><span class="function">BOOL <span class="title">RunService</span><span class="params">(<span class="keyword">char</span> * sSvcName)</span></span>;</div><div class="line"><span class="function">VOID <span class="title">ResumeService</span><span class="params">()</span></span>;</div><div class="line"><span class="function">VOID <span class="title">PauseService</span><span class="params">()</span></span>;</div><div class="line"><span class="function">VOID <span class="title">StopService</span><span class="params">()</span></span>;</div><div class="line"><span class="comment">//</span></div><div class="line"><span class="function">BOOL <span class="title">SendStatusToSCM</span> <span class="params">(DWORD dwCurrentState,</span></span></div><div class="line">   DWORD dwWin32ExitCode, </div><div class="line">   DWORD dwServiceSpecificExitCode,</div><div class="line">   DWORD dwCheckPoint,</div><div class="line">   DWORD dwWaitHint);</div><div class="line"><span class="comment">//</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">InstallService</span><span class="params">()</span></span>;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">UninstallService</span><span class="params">()</span></span>;</div><div class="line"><span class="comment">//</span></div><div class="line"><span class="function">DWORD WINAPI <span class="title">ServiceThread</span><span class="params">(LPDWORD param)</span></span>;</div><div class="line"></div><div class="line"><span class="comment">//安装入口点</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span>* argv[])</span></span></div><div class="line">&#123;</div><div class="line">    SERVICE_TABLE_ENTRY ServiceTable[<span class="number">2</span>];</div><div class="line">    <span class="comment">//设置服务名称和服务入口函数  </span></div><div class="line">    ServiceTable[<span class="number">0</span>].lpServiceName = ServiceName;</div><div class="line">    ServiceTable[<span class="number">0</span>].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;</div><div class="line">    ServiceTable[<span class="number">1</span>].lpServiceName = <span class="literal">NULL</span>;</div><div class="line">    ServiceTable[<span class="number">1</span>].lpServiceProc = <span class="literal">NULL</span>;</div><div class="line"></div><div class="line">    <span class="comment">//引数 </span></div><div class="line">    <span class="keyword">if</span>(argc&lt;=<span class="number">1</span>)</div><div class="line">    &#123;</div><div class="line">          <span class="built_in">cout</span>&lt;&lt;<span class="string">"使用方法:"</span>&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">          <span class="built_in">cout</span>&lt;&lt;<span class="string">"\"install\""</span>; </div><div class="line">          <span class="built_in">cout</span>&lt;&lt;<span class="string">"or"</span>;</div><div class="line">          <span class="built_in">cout</span>&lt;&lt;<span class="string">"\"uninstall\""</span>;</div><div class="line">          <span class="built_in">cout</span>&lt;&lt;<span class="string">"\"exec \""</span>; </div><div class="line">          <span class="built_in">cout</span>&lt;&lt;<span class="string">" parameter required in the command line"</span>&lt;&lt;<span class="built_in">endl</span>;      </div><div class="line">    &#125; </div><div class="line">    <span class="keyword">if</span>(argc&gt;<span class="number">1</span>)</div><div class="line">    &#123;</div><div class="line">          <span class="keyword">if</span>(<span class="number">_</span>stricmp(<span class="string">"install"</span>,argv[<span class="number">1</span>])==<span class="number">0</span>)</div><div class="line">          &#123;   <span class="comment">//install(InstallService)</span></div><div class="line">              InstallService(); </div><div class="line">              <span class="built_in">cout</span>&lt;&lt;<span class="string">"installService Succed!"</span>&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">          &#125;         </div><div class="line">          <span class="keyword">else</span> <span class="keyword">if</span>(<span class="number">_</span>stricmp(<span class="string">"uninstall"</span>,argv[<span class="number">1</span>])==<span class="number">0</span>)</div><div class="line">          &#123;   <span class="comment">//uninstall(UninstallService) </span></div><div class="line">              UninstallService(); </div><div class="line">              <span class="built_in">cout</span>&lt;&lt;<span class="string">"UninstallService Succed!"</span>&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">          &#125;       </div><div class="line">          <span class="keyword">else</span> <span class="keyword">if</span>(<span class="number">_</span>stricmp(<span class="string">"exec"</span>,argv[<span class="number">1</span>])==<span class="number">0</span>)</div><div class="line">          &#123;   <span class="comment">//exec(ServiceMain)</span></div><div class="line">              RunService(ServiceName);         </div><div class="line">          &#125; </div><div class="line">          <span class="keyword">else</span></div><div class="line">          &#123;    <span class="comment">//usage(使用方法)表示</span></div><div class="line">               <span class="built_in">cout</span>&lt;&lt;<span class="string">"使用方法 :"</span>&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">               <span class="comment">//"install"</span></div><div class="line">               <span class="built_in">cout</span>&lt;&lt;<span class="string">"\"install\""</span>; </div><div class="line">               <span class="comment">//"or"</span></div><div class="line">               <span class="built_in">cout</span>&lt;&lt;<span class="string">"or"</span>;</div><div class="line">               <span class="comment">//"uninstall"</span></div><div class="line">               <span class="built_in">cout</span>&lt;&lt;<span class="string">"\"uninstall\""</span>;</div><div class="line">               <span class="comment">//"exec"</span></div><div class="line">               <span class="built_in">cout</span>&lt;&lt;<span class="string">"\"exec \""</span>; </div><div class="line">               <span class="comment">//"or"</span></div><div class="line">               <span class="built_in">cout</span>&lt;&lt;<span class="string">"or"</span>;</div><div class="line">               <span class="built_in">cout</span>&lt;&lt;<span class="string">" parameter required in the command line"</span>&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">          &#125;</div><div class="line">    &#125;</div><div class="line">     <span class="comment">//-------------------------------------------------</span></div><div class="line">     <span class="comment">//启动服务的控制分派机线程(启动并执行一个新线程)</span></div><div class="line">     StartServiceCtrlDispatcher(ServiceTable);</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">//服务入口点(主线程)</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">ServiceMain</span><span class="params">(<span class="keyword">int</span> argc,<span class="keyword">char</span>** argv)</span></span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">int</span> success;</div><div class="line">    <span class="comment">//开始服务</span></div><div class="line">    RunService(ServiceName);</div><div class="line"></div><div class="line">    <span class="comment">//注册服务控制处理函数</span></div><div class="line">   hStatus = RegisterServiceCtrlHandler(ServiceName,(LPHANDLER_FUNCTION)ControlHandler);   </div><div class="line">   <span class="keyword">if</span>(!hStatus)</div><div class="line">   &#123;    </div><div class="line">       WriteToLog(<span class="string">"ServiceMain函数中注册服务控制处理函数失败!\n"</span>);</div><div class="line">       <span class="keyword">return</span>;</div><div class="line">   &#125;</div><div class="line">    <span class="comment">//向SCM报告工作状态</span></div><div class="line">    RunState=EnumRunState::Running;</div><div class="line">    ServiceStatus.dwCurrentState=SERVICE_RUNNING;</div><div class="line">    ServiceStatus.dwServiceType=SERVICE_WIN32;</div><div class="line">    ServiceStatus.dwControlsAccepted = </div><div class="line">            SERVICE_ACCEPT_STOP |</div><div class="line">            SERVICE_ACCEPT_PAUSE_CONTINUE |</div><div class="line">            SERVICE_ACCEPT_SHUTDOWN;</div><div class="line">    <span class="comment">/*SetServiceStatus(hStatus,&amp;ServiceStatus);*/</span></div><div class="line">    </div><div class="line">   <span class="comment">////启动工作循环(这个模块在另外一个线程中执行)</span></div><div class="line">   <span class="comment">//while(RunState==EnumRunState::Running)</span></div><div class="line">   <span class="comment">//&#123;</span></div><div class="line">      <span class="comment">//  WriteToLog("ServiceMain函数写日志\n");</span></div><div class="line">   <span class="comment">//     Sleep(500);</span></div><div class="line">   <span class="comment">//&#125;</span></div><div class="line">   <span class="comment">//创建一个终结事件</span></div><div class="line">   terminateEvent = CreateEvent (<span class="number">0</span>,TRUE,FALSE,<span class="number">0</span>);</div><div class="line">   <span class="keyword">if</span> (!terminateEvent)</div><div class="line">   &#123;</div><div class="line">       terminate(GetLastError());</div><div class="line">       <span class="keyword">return</span>;</div><div class="line">   &#125;   </div><div class="line">   <span class="comment">//</span></div><div class="line">   success =  SendStatusToSCM(SERVICE_START_PENDING,NO_ERROR,<span class="number">0</span>,<span class="number">2</span>,<span class="number">1000</span>);</div><div class="line">   <span class="keyword">if</span> (!success)</div><div class="line">   &#123;</div><div class="line">        terminate(GetLastError()); </div><div class="line">        <span class="keyword">return</span>;</div><div class="line">   &#125;</div><div class="line">   <span class="comment">//初始化</span></div><div class="line">   success =  InitService();</div><div class="line">   <span class="keyword">if</span> (!success)</div><div class="line">   &#123;</div><div class="line">        terminate(GetLastError());</div><div class="line">        <span class="keyword">return</span>;</div><div class="line">   &#125;</div><div class="line"></div><div class="line">   success = SendStatusToSCM(SERVICE_RUNNING,NO_ERROR,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>);</div><div class="line">   <span class="keyword">if</span> (!success)</div><div class="line">   &#123;</div><div class="line">        terminate(GetLastError()); </div><div class="line">        <span class="keyword">return</span>;</div><div class="line">   &#125;</div><div class="line"></div><div class="line">   <span class="comment">//等待停止</span></div><div class="line">   WaitForSingleObject ( terminateEvent, INFINITE);</div><div class="line">      </div><div class="line">   terminate(<span class="number">0</span>);</div><div class="line">   WriteToLog(<span class="string">"ServiceMain函数执行完毕!"</span>);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//服务控制台命令处理函数</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">ControlHandler</span><span class="params">(DWORD request)</span></span></div><div class="line">&#123;</div><div class="line">   DWORD  currentState = <span class="number">0</span>;</div><div class="line">   BOOL success; </div><div class="line">   WriteToLog(<span class="string">"ControlHandler:"</span>+request);</div><div class="line">   <span class="keyword">switch</span>(request)</div><div class="line">   &#123; </div><div class="line">       <span class="keyword">case</span> SERVICE_CONTROL_STOP:</div><div class="line">            RunState=EnumRunState::Stoped;</div><div class="line">            currentState = SERVICE_STOP_PENDING;</div><div class="line">            ServiceStatus.dwCurrentState=currentState;</div><div class="line">            SetServiceStatus(hStatus,&amp;ServiceStatus);           </div><div class="line">            StopService();</div><div class="line">            <span class="keyword">return</span>;</div><div class="line">       <span class="keyword">case</span> SERVICE_CONTROL_PAUSE:  </div><div class="line">             RunState=EnumRunState::Pause;</div><div class="line">             currentState = SERVICE_PAUSED;</div><div class="line">             ServiceStatus.dwCurrentState=currentState;</div><div class="line">             SetServiceStatus(hStatus,&amp;ServiceStatus);</div><div class="line">             PauseService();</div><div class="line">            <span class="keyword">break</span>;</div><div class="line">       <span class="keyword">case</span> SERVICE_CONTROL_CONTINUE:   </div><div class="line">           RunState=EnumRunState::Running;</div><div class="line">             currentState = SERVICE_RUNNING;</div><div class="line">             ServiceStatus.dwCurrentState=currentState;</div><div class="line">             SetServiceStatus(hStatus,&amp;ServiceStatus);</div><div class="line">             ResumeService();</div><div class="line">            <span class="keyword">break</span>;</div><div class="line">       <span class="keyword">case</span> SERVICE_CONTROL_INTERROGATE:</div><div class="line">            <span class="comment">// it will fall to bottom and send status</span></div><div class="line">            <span class="keyword">break</span>;</div><div class="line">        </div><div class="line">        <span class="comment">// Do nothing in a shutdown. Could do cleanup</span></div><div class="line">        <span class="comment">// here but it must be very quick.</span></div><div class="line">       <span class="keyword">case</span> SERVICE_CONTROL_SHUTDOWN:</div><div class="line">            <span class="comment">// Do nothing on shutdown          </span></div><div class="line">            <span class="keyword">return</span>;</div><div class="line">       <span class="keyword">default</span>:</div><div class="line">            <span class="keyword">break</span>;</div><div class="line">   &#125;   </div><div class="line">   <span class="comment">//ServiceStatus.dwCurrentState=currentState;</span></div><div class="line">   <span class="comment">//SetServiceStatus(hStatus,&amp;ServiceStatus);</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//初始化服务(开始服务时执行)</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">InitService</span><span class="params">()</span></span></div><div class="line">&#123;</div><div class="line">   <span class="keyword">int</span> result;</div><div class="line">   <span class="comment">//写日志信息</span></div><div class="line">   result=WriteToLog(<span class="string">"Monitoring started."</span>); </div><div class="line">   <span class="comment">//开始一个线程 Start the service's thread</span></div><div class="line">   DWORD id;   </div><div class="line">   threadHandle = CreateThread(<span class="number">0</span>, <span class="number">0</span>,(LPTHREAD_START_ROUTINE)ServiceThread,<span class="number">0</span>, <span class="number">0</span>, &amp;id);   </div><div class="line">   <span class="keyword">if</span> (threadHandle==<span class="number">0</span>)</div><div class="line">   &#123;</div><div class="line">       result=<span class="number">0</span>;</div><div class="line">   &#125;</div><div class="line">   <span class="keyword">else</span></div><div class="line">   &#123;        </div><div class="line">       result=<span class="number">1</span>;</div><div class="line">   &#125;</div><div class="line">   <span class="comment">//</span></div><div class="line">   WriteToLog(<span class="string">"InitService一个线程开始了!"</span>);</div><div class="line">   <span class="keyword">return</span> result;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function">DWORD WINAPI <span class="title">ServiceThread</span><span class="params">(LPDWORD param)</span></span></div><div class="line">&#123; </div><div class="line">   <span class="comment">//线程操作</span></div><div class="line">    <span class="keyword">while</span>(RunState==EnumRunState::Running)</div><div class="line">    &#123;</div><div class="line">        WriteToLog(<span class="string">"线程中写日志\n"</span>);</div><div class="line">        Sleep(<span class="number">500</span>);</div><div class="line">    &#125;</div><div class="line">   WriteToLog(<span class="string">"线程操作ServiceThread完成!"</span>);</div><div class="line">   <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="function">BOOL <span class="title">RunService</span><span class="params">(<span class="keyword">char</span> * sSvcName)</span></span></div><div class="line">&#123;   </div><div class="line">   SC_HANDLE schSCManager;</div><div class="line">   SC_HANDLE scHandle;</div><div class="line">   BOOL boolRet;</div><div class="line"></div><div class="line">   <span class="comment">// open to SCM   </span></div><div class="line">   schSCManager = OpenSCManager(</div><div class="line">        <span class="number">0</span>,  </div><div class="line">        <span class="number">0</span>,  </div><div class="line">        SC_MANAGER_ALL_ACCESS </div><div class="line">    );</div><div class="line"></div><div class="line">   <span class="comment">//Open</span></div><div class="line">   scHandle = OpenService(</div><div class="line">        schSCManager,              </div><div class="line">        sSvcName, </div><div class="line">        SERVICE_ALL_ACCESS  </div><div class="line">        );</div><div class="line">   </div><div class="line">   <span class="comment">//start </span></div><div class="line">   boolRet = StartService(</div><div class="line">        scHandle,          </div><div class="line">        <span class="number">0</span>,        </div><div class="line">        <span class="literal">NULL</span>); </div><div class="line">   </div><div class="line">   <span class="keyword">return</span> boolRet;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line"><span class="function">VOID <span class="title">ResumeService</span><span class="params">()</span></span></div><div class="line">&#123;</div><div class="line">   <span class="comment">//pauseService=FALSE;</span></div><div class="line">   <span class="comment">//ResumeThread(threadHandle);</span></div><div class="line">   <span class="keyword">return</span>;</div><div class="line">&#125;</div><div class="line"><span class="function">VOID <span class="title">PauseService</span><span class="params">()</span></span></div><div class="line">&#123;</div><div class="line">   <span class="comment">//pauseService = TRUE;</span></div><div class="line">   <span class="comment">//SuspendThread(threadHandle);</span></div><div class="line">   <span class="keyword">return</span>;</div><div class="line">&#125;</div><div class="line"><span class="function">VOID <span class="title">StopService</span><span class="params">()</span> </span></div><div class="line">&#123;</div><div class="line">    RunState=EnumRunState::Stoped;<span class="comment">//=FALSE;</span></div><div class="line">   <span class="comment">// Set the event that is holding ServiceMain</span></div><div class="line">   <span class="comment">// so that ServiceMain can return</span></div><div class="line">   SetEvent(terminateEvent);</div><div class="line">&#125;</div><div class="line"><span class="comment">//安装/反安装函数</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">InstallService</span><span class="params">()</span></span></div><div class="line">&#123; </div><div class="line">     SC_HANDLE schService; </div><div class="line">     SC_HANDLE schSCManager; </div><div class="line">     <span class="keyword">char</span> szPath[<span class="number">512</span>];</div><div class="line">     <span class="built_in">string</span> userName = <span class="string">"NT AUTHORITY\\NetworkService"</span>;</div><div class="line">     </div><div class="line">     <span class="comment">//Install Starting... </span></div><div class="line">     <span class="comment">//获取模块文件路径名称</span></div><div class="line">     <span class="keyword">if</span>(!GetModuleFileName(<span class="literal">NULL</span>,szPath,<span class="number">512</span>)) </div><div class="line">     &#123;  </div><div class="line">         <span class="keyword">return</span>;  </div><div class="line">     &#125;</div><div class="line">     </div><div class="line">     <span class="comment">// open to SCM　</span></div><div class="line">     schSCManager = OpenSCManager( </div><div class="line">                                <span class="number">0</span>,  <span class="comment">// pointer to machine name string</span></div><div class="line">                                <span class="number">0</span>,  <span class="comment">// pointer to database name string</span></div><div class="line">                                SC_MANAGER_CREATE_SERVICE <span class="comment">// type of access</span></div><div class="line">                                );</div><div class="line">     <span class="keyword">if</span> (!schSCManager)</div><div class="line">     &#123;</div><div class="line">         WriteToLog(<span class="string">"OpenScManagerErrorID:"</span> + GetLastError());  </div><div class="line">         <span class="keyword">return</span>;</div><div class="line">     &#125;</div><div class="line">     <span class="comment">// Install 新服务</span></div><div class="line">     schService = CreateService(</div><div class="line">            schSCManager, <span class="comment">// handle to service control manager database</span></div><div class="line">            ServiceName, <span class="comment">// pointer to name of service to start</span></div><div class="line">            ServiceName, <span class="comment">// pointer to display name     </span></div><div class="line">            SERVICE_ALL_ACCESS,<span class="comment">// type of access to service</span></div><div class="line">            SERVICE_WIN32_OWN_PROCESS,<span class="comment">// type of service</span></div><div class="line">            SERVICE_DEMAND_START,<span class="comment">// when to start service</span></div><div class="line">            SERVICE_ERROR_NORMAL,<span class="comment">// severity if service fails to start</span></div><div class="line">            szPath,       <span class="comment">// pointer to name of binary file</span></div><div class="line">            <span class="literal">NULL</span>,         <span class="comment">// pointer to name of load ordering group</span></div><div class="line">            <span class="literal">NULL</span>,         <span class="comment">// pointer to variable to get tag identifier</span></div><div class="line">            <span class="literal">NULL</span>,         <span class="comment">// pointer to array of dependency names</span></div><div class="line">            userName.c_str(),         <span class="comment">// pointer to account name of service</span></div><div class="line">            <span class="literal">NULL</span>          <span class="comment">// pointer to password for service account</span></div><div class="line">            );</div><div class="line"></div><div class="line">     <span class="keyword">if</span> (!schService)</div><div class="line">     &#123;  </div><div class="line">         WriteToLog(<span class="string">"CreateServiceErrorID:"</span>+GetLastError()); </div><div class="line">          <span class="keyword">return</span>;</div><div class="line">     &#125;</div><div class="line">     <span class="keyword">else</span></div><div class="line">     &#123;</div><div class="line">         WriteToLog(<span class="string">"Service installed\n"</span>); </div><div class="line">     &#125;   </div><div class="line"></div><div class="line">     CloseServiceHandle(schService);</div><div class="line">     CloseServiceHandle(schSCManager);   </div><div class="line">     WriteToLog(<span class="string">"Install Ending...\n"</span>); </div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">UninstallService</span><span class="params">()</span></span></div><div class="line">&#123; </div><div class="line">     SC_HANDLE schService;</div><div class="line">     SC_HANDLE schSCManager; </div><div class="line">     BOOL success;</div><div class="line">     SERVICE_STATUS svcStatus;</div><div class="line">     </div><div class="line">     WriteToLog(<span class="string">"Uninstall Starting..."</span>);</div><div class="line">     </div><div class="line">     <span class="comment">// open to SCM　</span></div><div class="line">     schSCManager = OpenSCManager(</div><div class="line">            <span class="number">0</span>,<span class="comment">// pointer to machine name string </span></div><div class="line">            <span class="number">0</span>,<span class="comment">// pointer to database name string</span></div><div class="line">            SC_MANAGER_CREATE_SERVICE <span class="comment">// type of access</span></div><div class="line">            );</div><div class="line">     <span class="keyword">if</span> (!schSCManager)</div><div class="line">     &#123;</div><div class="line">         WriteToLog(<span class="string">"OpenScManagerErrorID:"</span>+GetLastError());</div><div class="line">         <span class="keyword">return</span>;</div><div class="line">     &#125;</div><div class="line">     </div><div class="line">     <span class="comment">//打开一个服务</span></div><div class="line">     schService = OpenService(</div><div class="line">                  schSCManager,              <span class="comment">// handle to service control manager database</span></div><div class="line">                  ServiceName,              <span class="comment">// pointer to name of service to start</span></div><div class="line">                  SERVICE_ALL_ACCESS | DELETE<span class="comment">// type of access to service</span></div><div class="line">                  );</div><div class="line">     <span class="keyword">if</span> (!schService)</div><div class="line">     &#123;</div><div class="line">          WriteToLog(<span class="string">"OpenServiceErrorID:"</span>+GetLastError());</div><div class="line">          <span class="keyword">return</span>;</div><div class="line">     &#125;</div><div class="line">     <span class="comment">//(if necessary)</span></div><div class="line">     success = QueryServiceStatus(schService, &amp;svcStatus);</div><div class="line">     <span class="keyword">if</span> (!success)</div><div class="line">     &#123;</div><div class="line">         WriteToLog(<span class="string">"In QueryServiceStatus ErrorID:"</span>+GetLastError());</div><div class="line">          <span class="keyword">return</span>;</div><div class="line">     &#125;</div><div class="line">     <span class="keyword">if</span> (svcStatus.dwCurrentState != SERVICE_STOPPED)</div><div class="line">     &#123;</div><div class="line">           WriteToLog(<span class="string">"Stopping service..."</span>);</div><div class="line">           success = ControlService(</div><div class="line">               schService,           <span class="comment">// handle to service</span></div><div class="line">               SERVICE_CONTROL_STOP, <span class="comment">// control code</span></div><div class="line">               &amp;svcStatus            <span class="comment">// pointer to service status structure</span></div><div class="line">           );</div><div class="line">           <span class="keyword">if</span> (!success)</div><div class="line">           &#123;</div><div class="line">               WriteToLog(<span class="string">"In ControlServiceErrorID:"</span>+GetLastError());</div><div class="line">               <span class="keyword">return</span>;</div><div class="line">           &#125;</div><div class="line">     &#125;</div><div class="line"></div><div class="line">     <span class="comment">//等待</span></div><div class="line">     <span class="keyword">do</span></div><div class="line">     &#123;</div><div class="line">          QueryServiceStatus(schService,&amp;svcStatus);      </div><div class="line">          Sleep(<span class="number">500</span>);    </div><div class="line">      </div><div class="line">     &#125;<span class="keyword">while</span>(SERVICE_STOP_PENDING==svcStatus.dwCurrentState);        </div><div class="line">     </div><div class="line">     <span class="keyword">if</span>(SERVICE_STOPPED!=svcStatus.dwCurrentState)</div><div class="line">     &#123;</div><div class="line">         WriteToLog(<span class="string">"Failed to Stop Service ErrorID:"</span>+GetLastError());  </div><div class="line">         <span class="keyword">return</span>;</div><div class="line">     &#125;</div><div class="line">     </div><div class="line">     <span class="comment">//删除服务 </span></div><div class="line">     success = DeleteService(schService);</div><div class="line">     <span class="keyword">if</span> (success)</div><div class="line">     &#123;</div><div class="line">         WriteToLog(<span class="string">"Service removed\n"</span>);</div><div class="line">     &#125;</div><div class="line">     <span class="keyword">else</span></div><div class="line">     &#123;</div><div class="line">         WriteToLog(<span class="string">"In DeleteService ErrorID:"</span>+GetLastError());</div><div class="line">         <span class="keyword">return</span>;</div><div class="line">     &#125;   </div><div class="line">     <span class="comment">//Clean up</span></div><div class="line">     CloseServiceHandle(schService);</div><div class="line">     CloseServiceHandle(schSCManager);   </div><div class="line">     WriteToLog(<span class="string">"Uninstal Ending..."</span>);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="function">BOOL <span class="title">SendStatusToSCM</span> <span class="params">(DWORD dwCurrentState,</span></span></div><div class="line">   DWORD dwWin32ExitCode, </div><div class="line">   DWORD dwServiceSpecificExitCode,</div><div class="line">   DWORD dwCheckPoint,</div><div class="line">   DWORD dwWaitHint)</div><div class="line">  &#123;</div><div class="line">       BOOL success;</div><div class="line">       SERVICE_STATUS serviceStatus;</div><div class="line">       </div><div class="line">       <span class="comment">// Fill in all of the SERVICE_STATUS fields</span></div><div class="line">       serviceStatus.dwServiceType =SERVICE_WIN32_OWN_PROCESS;</div><div class="line">       serviceStatus.dwCurrentState = dwCurrentState;</div><div class="line">       </div><div class="line">       <span class="comment">// If in the process of something, then accept</span></div><div class="line">       <span class="comment">// no control events, else accept anything</span></div><div class="line">       <span class="keyword">if</span> (dwCurrentState == SERVICE_START_PENDING)</div><div class="line">            serviceStatus.dwControlsAccepted = <span class="number">0</span>;</div><div class="line">       <span class="keyword">else</span></div><div class="line">            serviceStatus.dwControlsAccepted = </div><div class="line">            SERVICE_ACCEPT_STOP |</div><div class="line">            SERVICE_ACCEPT_PAUSE_CONTINUE |</div><div class="line">            SERVICE_ACCEPT_SHUTDOWN;</div><div class="line">       </div><div class="line">       <span class="comment">// if a specific exit code is defines, set up</span></div><div class="line">       <span class="comment">// the win32 exit code properly</span></div><div class="line">       <span class="keyword">if</span> (dwServiceSpecificExitCode == <span class="number">0</span>)</div><div class="line">            serviceStatus.dwWin32ExitCode =dwWin32ExitCode;</div><div class="line">       <span class="keyword">else</span></div><div class="line">            serviceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;</div><div class="line">       serviceStatus.dwServiceSpecificExitCode =dwServiceSpecificExitCode;</div><div class="line">       </div><div class="line">       serviceStatus.dwCheckPoint = dwCheckPoint;</div><div class="line">       serviceStatus.dwWaitHint = dwWaitHint;</div><div class="line">       </div><div class="line">       <span class="comment">// Pass the status record to the SCM</span></div><div class="line">       <span class="comment">//SERVICE_STATUS_HANDLE serviceStatusHandle;</span></div><div class="line">       success = SetServiceStatus (hStatus,&amp;serviceStatus);</div><div class="line">       <span class="keyword">if</span> (!success)</div><div class="line">       &#123;</div><div class="line">          StopService();     </div><div class="line">       &#125;</div><div class="line">       <span class="keyword">return</span> success;</div><div class="line">  &#125;</div><div class="line"><span class="comment">//</span></div><div class="line"><span class="function">VOID <span class="title">terminate</span><span class="params">(DWORD error)</span></span></div><div class="line">&#123;</div><div class="line">   <span class="comment">// if terminateEvent has been created, close it.</span></div><div class="line">   <span class="keyword">if</span> (terminateEvent)</div><div class="line">    CloseHandle(terminateEvent);</div><div class="line">   </div><div class="line">   <span class="comment">// Send a message to the scm to tell about</span></div><div class="line">   <span class="comment">// stop age</span></div><div class="line">   <span class="keyword">if</span> (hStatus)</div><div class="line">    SendStatusToSCM(SERVICE_STOPPED, error,</div><div class="line">    <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>);</div><div class="line">   </div><div class="line">   <span class="comment">// If the thread has started kill it off</span></div><div class="line">   <span class="keyword">if</span> (threadHandle)</div><div class="line">    CloseHandle(threadHandle);</div><div class="line">   </div><div class="line">   <span class="comment">// Do not need to close serviceStatusHandle</span></div><div class="line">&#125;</div></pre></td></tr></table></figure><p></p></div></div><div class="copyright"><p><span>本文标题:</span><a href="/backend/2015/09/28/window-service-program.html">window服务编程</a></p><p><span>文章作者:</span><a href="/" title="访问 lehoon 的个人博客">lehoon</a></p><p><span>发布时间:</span>2015年09月28日 - 19时40分</p><p><span>最后更新:</span>2016年07月28日 - 09时53分</p><p><span>原始链接:</span><a class="post-url" href="/backend/2015/09/28/window-service-program.html" title="window服务编程">http://blog.lehoon.cn/backend/2015/09/28/window-service-program.html</a><span class="copy-path" data-clipboard-text="原文: http://blog.lehoon.cn/backend/2015/09/28/window-service-program.html　　作者: lehoon" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span><script src="/js/clipboard.min.js"></script><script>var clipboard=new Clipboard(".copy-path")</script></p><p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/cn/" title="中国大陆 (CC BY-NC-SA 3.0 CN)" target="_blank">"署名-非商用-相同方式共享 3.0"</a> 转载请保留原文链接及作者。</p></div><nav id="article-nav"><a href="/orther/2015/10/11/kesankaoshi.html" id="article-nav-newer" class="article-nav-link-wrap"><strong class="article-nav-caption">&lt;</strong><div class="article-nav-title">机动车驾驶科目三操作要领</div></a><a href="/orther/2015/09/21/assembly-instruction.html" id="article-nav-older" class="article-nav-link-wrap"><div class="article-nav-title">汇编指令集</div><strong class="article-nav-caption">&gt;</strong></a></nav></article><div id="toc" class="toc-article"><strong class="toc-title">文章目录</strong></div><input type="button" id="tocButton" value="隐藏目录" title="点击按钮隐藏或者显示文章目录"><script src="http://7.url.cn/edu/jslib/comb/require-2.1.6,jquery-1.9.1.min.js"></script><script>var valueHide="隐藏目录",valueShow="显示目录";$(".left-col").is(":hidden")&&$("#tocButton").attr("value",valueShow),$("#tocButton").click(function(){$("#toc").is(":hidden")?($("#tocButton").attr("value",valueHide),$("#toc").slideDown(320)):($("#tocButton").attr("value",valueShow),$("#toc").slideUp(350))}),$(".toc").length<1&&$("#toc, #tocButton").hide()</script><div class="scroll" id="post-nav-button"><a href="/orther/2015/10/11/kesankaoshi.html" title="上一篇: 机动车驾驶科目三操作要领"><i class="fa fa-angle-left"></i></a><a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a><a href="/orther/2015/09/21/assembly-instruction.html" title="下一篇: 汇编指令集"><i class="fa fa-angle-right"></i></a></div><ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/orther/2017/02/22/source-insight4-crack.html">source insight4安装使用</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2017/02/21/rocketmq-topic-delete.html">rocketmq删除topic</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2017/02/21/rocketmq-message-store.html">rocketmq消息存储</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2017/02/20/idempotent-define.html">幂等介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2017/02/20/github-sync-fork-upstream.html">github上fork项目源同步</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2017/02/16/intellij-idea-2016.html">intellij-idea-2016下载安装</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2017/01/24/bubiao-protocol.html">部标808协议文件</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2017/01/24/life-secret.html">生活小秘方</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2017/01/10/mysql-odbc-driver-error.html">无法加载mysql odbc 5.3 unicode driver</a></li><li class="post-list-item"><a class="post-list-link" href="/rocketmq/2016/11/25/rocketmq-net-port.html">rocketmq服务中的端口号</a></li><li class="post-list-item"><a class="post-list-link" href="/rocketmq/2016/11/13/rocketmq-broker-synctopic2namesrv.html">rocketmq中broker启动同步topic到namesrv过程</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2016/11/10/java-error-note.html">java常见错误</a></li><li class="post-list-item"><a class="post-list-link" href="/rocketmq/2016/11/09/rocketmq-install-note.html">rocketmq安装记录</a></li><li class="post-list-item"><a class="post-list-link" href="/rocketmq/2016/11/09/rocketmq-docs.html">Rocketmq文档</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2016/10/28/game-develop-skill.html">游戏开发要点</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/10/14/oracle-error-note.html">oracle错误记录</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/10/11/oracle-materialized-view.html">oracle物化视图</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/10/09/centos-oracle11gr2-install.html">Centos7安装oracle11gr2</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/10/08/gdb-miss-debuginfo.html">gdb-miss-debuginfo</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/09/29/linux-io-schedule.html">Linux IO调度算法</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/09/27/linux-c-func.html">Linux下C函数定义</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/09/26/tcpdump.html">tcpdump使用文档</a></li><li class="post-list-item"><a class="post-list-link" href="/编程/2016/09/20/eim.html">eim</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/09/16/linux-strace.html">linux-strace使用介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/09/16/linux-brk-sbrk.html">brk,sbrk--改变数据段长度</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/09/07/centos-note.html">Centos记录</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2016/09/05/raid.html">raid磁盘阵列介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/09/02/nginx-load-balance.html">nginx负载均衡各种场景下的应用</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2016/09/01/usb-centos-error.html">U盘安装centos7，启动报错</a></li><li class="post-list-item"><a class="post-list-link" href="/frontend/2016/09/01/js-generate-uuid.html">JavaScript生成全局唯一id的方法</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2016/08/29/notepad.html">随笔</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/08/22/kafka-window.html">kafka在window下部署</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/08/19/makefile.html">Makefile学习</a></li><li class="post-list-item"><a class="post-list-link" href="/frontend/2016/08/16/http-etag-last-midified.html">HTTP缓存ETAG和Last-Modified</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2016/08/16/ip-catagory.html">ip地址分类</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2016/08/09/android-connection-session.html">android非浏览器应用程序与服务器会话保持实现</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/07/28/memcache-memcached-diff.html">memcache与memcached区别</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/07/19/centos-lvs.html">CentOS 7.0下安装部署LVS</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2016/07/19/awk.html">awk学习</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/07/11/epoll.html">Linux epoll学习</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/07/04/golang-deamon.html">go守护进程实现方式</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/07/04/tcp-load-balancing.html">tcp连接负载均衡</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/07/04/tcp-rst.html">tcp出现rst情况</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2016/05/26/linux-tcp-optimization.html">Linux下tcp的优化</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2016/05/08/oracle-merge-into.html">oracle中merge into语法注意点</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2016/02/05/window-batch.html">Window批处理记录</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2015/10/11/kesankaoshi.html">机动车驾驶科目三操作要领</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2015/09/28/window-service-program.html">window服务编程</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2015/09/21/assembly-instruction.html">汇编指令集</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2015/09/15/bool-BOOL.html">bool和BOOL的区别</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2015/08/20/ssh-limit.html">ssh限制ip远程登录</a></li><li class="post-list-item"><a class="post-list-link" href="/orther/2015/08/18/ue-regex.html">ultraedit正则表达式使用</a></li><li class="post-list-item"><a class="post-list-link" href="/backend/2015/05/07/occi.html">c++使用occi连接oracle</a></li><li class="post-list-item"><a class="post-list-link" href="/frontend/2015/03/06/gulp.html">gulp 提高页面加载速度</a></li></ul><script src="http://7.url.cn/edu/jslib/comb/require-2.1.6,jquery-1.9.1.min.js"></script><script>$(".post-list").addClass("toc-article"),$(".post-list-item a").attr("target","_blank"),$("#post-nav-button > a:nth-child(2)").click(function(){$(".fa-bars, .fa-times").toggle(),$(".post-list").toggle(300),$(".toc").length>0&&$("#toc, #tocButton").toggle(200,function(){$(".switch-area").is(":visible")&&$("#tocButton").attr("value",valueHide)})})</script><script></script></div><footer id="footer"><div class="outer"><div id="footer-info"><div class="footer-left">&copy; 2017 lehoon</div><div class="footer-right"><a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/luuman/hexo-theme-spfk" target="_blank">spfk</a> by luuman</div></div></div></footer></div><script src="http://7.url.cn/edu/jslib/comb/require-2.1.6,jquery-1.9.1.min.js"></script><script src="http://static.lehoon.cn/js/main.js"></script><script>$(document).ready(function(){})</script><div class="scroll" id="scroll"><a href="#"><i class="fa fa-arrow-up"></i></a><a href="#comments"><i class="fa fa-comments-o"></i></a><a href="#footer"><i class="fa fa-arrow-down"></i></a></div><script>$(document).ready(function(){$("#comments").length<1&&$("#scroll > a:nth-child(2)").hide()})</script><script language="javascript">$(function(){$("a[title]").each(function(){var t=$(this),e=t.attr("title");void 0!=e&&""!=e&&t.data("title",e).removeAttr("title").hover(function(){var i=t.offset();$('<div id="anchortitlecontainer"></div>').appendTo($("body")).html(e).css({top:i.top-t.outerHeight()-15,left:i.left+t.outerWidth()/2+1}).fadeIn(function(){var t=$(this);setTimeout(function(){t.remove()},800*t.text().length)})},function(){$("#anchortitlecontainer").remove()})})})</script></div></body></html>